//
//  MainViewController.m
//  doubanFun
//
//  Created by bing on 16/3/24.
//  Copyright © 2016年 bing. All rights reserved.
//

#import "RESTClient.h"
#import "AppDelegate.h"
#import "MainPageView.h"
#import "MainViewController.h"
#import "PlayViewController.h"
#import "ChannelsViewController.h"

#define palyViewHeight 49
#define navigationHeight 64
#define channelViewOffset 200

@interface MainViewController ()
<PlayViewControllerDelegate>

@property (nonatomic, strong) UIViewController *channelVC;
@property (nonatomic, strong) UIViewController *playVC;
@property (nonatomic, weak) MainPageView *mainView;
@property (nonatomic, weak) UIView *coverView;
@property (nonatomic, assign) BOOL channelViewShow;
@end

@implementation MainViewController

- (void)setup {
    // 添加背景
    UINib *nib = [UINib nibWithNibName:@"MainPageView" bundle:nil];
    MainPageView *mainView = [nib instantiateWithOwner:nib options:nil].firstObject;
    mainView.frame = [UIScreen mainScreen].bounds;
    mainView.userInteractionEnabled = YES;
    [self.view addSubview:mainView];
    _mainView = mainView;
    
    // 添加播放
    CGRect rect = CGRectMake(0, SCREEN_HEIGHT - palyViewHeight, SCREEN_WIDTH, palyViewHeight);
    PlayViewController *playVC = [[PlayViewController alloc] init];
    playVC.view.autoresizingMask = UIViewAutoresizingNone;
    [self addChildViewController:playVC];
    [self.view addSubview:playVC.view];
    playVC.view.layer.shadowOffset = CGSizeMake(0, 3);
    playVC.view.layer.shadowOpacity = 0.5;
    playVC.view.frame = rect;
    playVC.view.alpha = 0.4;
    playVC.delegate = self;
    _playVC = playVC;
    
    // 添加频道
    rect = CGRectMake(-channelViewOffset, 0, channelViewOffset, SCREEN_HEIGHT);
    ChannelsViewController *channelVC = [[ChannelsViewController alloc] init];
    channelVC.view.autoresizingMask = UIViewAutoresizingNone;
    [self addChildViewController:channelVC];
    [self.view addSubview:channelVC.view];
    channelVC.view.frame = rect;
    channelVC.view.alpha = 0.7;
    channelVC.delegate = playVC;
    _channelVC = channelVC;
    
    // 添加侧边滑动手势
    UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] init];
    [edgePan addTarget:self action:@selector(showChannelsView:)];
    edgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:edgePan];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


#pragma mark - UIScreenEdgePanGestureRecognizer

- (void)showChannelsView:(UIScreenEdgePanGestureRecognizer *)sender {
    
    UIView *_channelView = _channelVC.view;
    // 手势变换
    if (sender.state == UIGestureRecognizerStateChanged && !_channelViewShow)
    {
        CGPoint translation = [sender translationInView:sender.view];
        if (translation.x > channelViewOffset) {
            return;
        }
        [UIView animateWithDuration:0.1 animations:^{
            _channelView.center = CGPointMake(translation.x - channelViewOffset * 0.5, _channelView.center.y);
        }];
        
    }
    // 手势结束
    else if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint translation = [sender translationInView:sender.view];
        // 打开的情况
        if (_channelViewShow)
        {
            CGPoint point = CGPointMake(channelViewOffset * 0.5, _channelView.center.y);
            if (translation.x < channelViewOffset * 0.5) {
                point = CGPointMake(-channelViewOffset * 0.5, _channelView.center.y);
            }
            [UIView animateWithDuration:0.2 animations:^{
                _channelView.center = point;
            } completion:^(BOOL finished) {
                _channelViewShow = NO;
            }];
        }
        // 关闭的情况
        else
        {
            CGPoint point = CGPointMake(-channelViewOffset * 0.5, _channelView.center.y);
            if (translation.x > channelViewOffset * 0.5) {
                point = CGPointMake(channelViewOffset * 0.5, _channelView.center.y);
            }
            [UIView animateWithDuration:0.2 animations:^{
                _channelView.center = point;
            } completion:^(BOOL finished) {
                _channelViewShow = YES;
                [self addCoverView];
            }];
        }
    }
}

- (void)addCoverView {
    if (_coverView) {
        return;
    }
    UIView *coverView = [[UIView alloc] initWithFrame:self.view.bounds];
    coverView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeChannelView)];
    [coverView addGestureRecognizer:tap];
    [self.view insertSubview:coverView belowSubview:_channelVC.view];
    _coverView = coverView;
}

- (void)closeChannelView {
    [UIView animateWithDuration:0.2 animations:^{
        _channelVC.view.center = CGPointMake(-channelViewOffset * 0.5, _channelVC.view.center.y);
    } completion:^(BOOL finished) {
        _channelViewShow = NO;
    }];
    [_coverView removeFromSuperview];
    _coverView = nil;
}


#pragma mark - PlayViewControllerDelegate

- (void)viewController:(PlayViewController *)playVC playSong:(DFSong *)song {
    [self.mainView setSongModal:song];
}


@end
